package it.unipd.fbaron.mind_reader_squared.activities;

import it.unipd.fbaron.mind_reader_squared.R;
import it.unipd.fbaron.mind_reader_squared.model.ProfileLogic;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MenuActivity extends BaseActivity {

	Button m1Button;
	Button m2Button;
	Button m3Button;
	Button configSymbolButton;
	Button configProfileButton;

	boolean resumedWithBack = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_layout);

		m1Button = (Button) findViewById(R.id.menu_m1_button);
		m2Button = (Button) findViewById(R.id.menu_m2_button);
		m3Button = (Button) findViewById(R.id.menu_m3_button);
		configSymbolButton = (Button) findViewById(R.id.menu_config_symbol_button);
		configProfileButton = (Button) findViewById(R.id.menu_config_profile_button);

		String[] result = ProfileLogic.getInstance(dbHelper).queryWithId(
				profileId, new String[] { "open_m2", "open_m3" });
		if (Integer.parseInt(result[0]) == 0) {
			m2Button.setClickable(false);
			m2Button.setText(m2Button.getText() + "  (LOCKED)");
		}
		if (Integer.parseInt(result[1]) == 0) {
			m3Button.setClickable(false);
			m3Button.setText(m3Button.getText() + "  (LOCKED)");
		}
		
		m1Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MenuActivity.this, M1Activity.class);
				intent.putExtra("profile_id",
						getIntent().getIntExtra("profile_id", -1));
				startActivity(intent);
			}
		});
		
		m3Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MenuActivity.this, M3Activity.class);
				intent.putExtra("profile_id",
						getIntent().getIntExtra("profile_id", -1));
				startActivity(intent);
			}
		});

		configSymbolButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MenuActivity.this,
						ManageSymbolActivity.class);
				intent.putExtra("profile_id",
						getIntent().getIntExtra("profile_id", -1));
				startActivity(intent);
			}
		});

		configProfileButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MenuActivity.this,
						ManageProfileActivity.class);
				intent.putExtra("profile_id",
						getIntent().getIntExtra("profile_id", -1));
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (resumedWithBack) {
			// Restart activity
			Intent intent = getIntent();
			overridePendingTransition(0, 0);
			finish();
			overridePendingTransition(0, 0);
			startActivity(intent);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();

		resumedWithBack = true;
	}

}
